import { Entity, PrimaryGeneratedColumn, Column, CreateDateColumn, UpdateDateColumn, ManyToMany, JoinTable } from 'typeorm';
import { Tag } from './tag.entity';

@Entity()
export class Article {
  @PrimaryGeneratedColumn()
  id: number;

  @Column()
  title: string;

  @Column()
  content: string;

  @Column({ nullable: true })
  coverImage: string;

  @Column({ default: 0 })
  viewCount: number;

  @Column({ default: 0 })
  likeCount: number;

  @Column({ default: 0 })
  commentCount: number;
  
  @Column({ type: 'int', default: -1 }) // -1: 待审核, 0: 审核失败, 1: 审核成功
  isActive: number;
  
  @Column({ nullable: true })
  authorId: number;

  @Column({ nullable: true })
  categoryId: number;

  @CreateDateColumn()
  createdAt: Date;

  @UpdateDateColumn()
  updatedAt: Date;


    // 多对多关系
  @ManyToMany(() => Tag)
  @JoinTable({
    name: 'article_tags', // 关系表名
    joinColumn: { name: 'article_id', referencedColumnName: 'id' },
    inverseJoinColumn: { name: 'tag_id', referencedColumnName: 'id' }
  })
  tags: Tag[];

  // 定义状态常量，方便使用
  static readonly STATUS_PENDING = -1; // 待审核
  static readonly STATUS_REJECTED = 0; // 审核失败 / 删除
  static readonly STATUS_APPROVED = 1; // 审核成功
}